/**
 * 
 */
package cn.com.shiro.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import cn.com.shiro.dao.UserDao;
import cn.com.shiro.vo.User;

/**
 * @className 
 * @descripion 
 * @author WangJun
 * @date 2018年6月29日上午10:41:45
 */
@Component
public class UserDaoImpl implements UserDao{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public User getUserByName(String userName) {
		// TODO Auto-generated method stub
		String sql ="select username,password from users where username = ?";
		List<User> list = jdbcTemplate.query(sql, new String[] {userName}, new RowMapper<User>() {
			@Override
			public User mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				User user = new User();
				user.setUsername(arg0.getString("username"));
				user.setPassword(arg0.getString("password"));
				return user;
			}
		});
		if(CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}
	@Override
	public List<String> queryRolesByName(String userName) {
		// TODO Auto-generated method stub
		String sql = "select role_name from user_roles where username = ?";
		return  jdbcTemplate.query(sql, new String[] {userName}, new RowMapper<String>() {

			@Override
			public String mapRow(ResultSet arg0, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				return arg0.getString("role_name");
			}
			
		});
	}
	@Override
	public List<String> queryPermsByName(String userName) {
		// TODO Auto-generated method stub
		String sql = "select permission from roles_permissions where role_name = ?";
		return jdbcTemplate.queryForList(sql, new String[] {userName}, String.class);
	}

}
